﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;
using System.Diagnostics;

namespace ClientManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (Workers == null)
            {
                Workers = new List<WCFLabWorker>();

                var minimumDelay = int.Parse(InitialDelay.Text);
                var maximumDelay = int.Parse(FinalDelay.Text);
                var numberOfClients = int.Parse(NumberOfClients.Text);

                ParallelEnumerable.Range(minimumDelay, maximumDelay)
                    .Where(tNum => (tNum % 100) == 0)
                    .ToList()
                    .ForEach(tNum =>
                        {
                            Enumerable.Range(1, numberOfClients).ToList()
                                .ForEach(clientNumber =>
                                Workers.Add(GetWorker(string.Format(@"{0}-{1}", tNum, clientNumber), tNum, Dispatcher)));
                        });
            }

            Workers.AsParallel().ForAll(tWorker => tWorker.RunWorkerAsync());

            Start.IsEnabled = false;
            Stop.IsEnabled = true;
            Restart.IsEnabled = false;
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            Workers.AsParallel().ForAll(tWorker => tWorker.CancelAsync());

            Start.IsEnabled = true;
            Stop.IsEnabled = false;
            Restart.IsEnabled = true;
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            Workers.AsParallel().ForAll(tWorker => tWorker.Dispose());

            Workers = null;

            Start.IsEnabled = true;
            Stop.IsEnabled = false;
            Restart.IsEnabled = false;

            LogText.Text = string.Empty;
        }

        protected void worker_NotifyRepetition(object sender, EventArgs e)
        {
            var wcfArgs = e as WCFWorkerArgs;

            LogText.AppendText(string.Format("Repetition {0} from Client {1} (Session: {2}) was processed at {3}\n", wcfArgs.Repetition, wcfArgs.ClientId, wcfArgs.SessionId, wcfArgs.StatusTimestamp.ToString("hh:mm:ss.fffffff")));
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private List<WCFLabWorker> Workers;

        private WCFLabWorker GetWorker(string clientId, int delay, Dispatcher dispatcher)
        {
            var worker = new WCFLabWorker() { ClientId = clientId, Delay = delay, Dispatcher = this.Dispatcher };

            worker.DoWork += new DoWorkEventHandler(worker.worker_DoWork);

            worker.NotifyRepetition += new EventHandler(worker_NotifyRepetition);

            return worker;
        }
    }
}
